﻿using System;
using System.Collections.Generic;
using System.Transactions;
using Bingosoft.Data;
using HHMMS.Core.Model;

namespace HHMMS.Core.Service
{
    public class PartsDiscardService
    {
        private readonly Dao _dao = Dao.Get();

        public PartsDiscardRecord GetPartsDiscardRecord(Int32 Id)
        {
            var param = new Dictionary<string, object>() { { "Id", Id } };
            return this._dao.QueryEntity<PartsDiscardRecord>("PlanManager.PartsDiscardRecord.Get", param);                
        }

        public int SaveOrUpdatePartsDiscardRecord(PartsDiscardRecord partsDiscardRecord)
        {
            using (TransactionScope transactionScope = new TransactionScope(TransactionScopeOption.Required))
            {
                if (!partsDiscardRecord.Id.HasValue)
                {
                    partsDiscardRecord.Id = this._dao.QueryScalar<int>("PlanManager.PartsDiscardRecord.Insert", partsDiscardRecord);
                }
                else
                {
                    this._dao.ExecuteNonQuery("PlanManager.PartsDiscardRecord.Update", partsDiscardRecord);
                }
                transactionScope.Complete();
            }
            return partsDiscardRecord.Id ?? 0;
        }

        public void DeletePartsDiscardRecord(string Id)
        {
            using (TransactionScope transactionScope = new TransactionScope())
            {
                var param = new Dictionary<string, object>() { { "Id", Id } };
                this._dao.ExecuteNonQuery("PlanManager.PartsDiscardRecord.Delete", param);
                transactionScope.Complete();
            }

        }
        
    }
}